﻿--添加单位
CREATE PROCEDURE [dbo].[proc_zzjg_xx_CreateCompany]
	(
	 @Companyid varchar(500),	
	 @id INT	
	)
AS
	begin 

BEGIN TRANSACTION--开始事务 
DECLARE @errorSun INT --定义错误计数器
SET @errorSun=0 --没错为0
                --
---------生成新编号 BEGIN--------
DECLARE @bh VARCHAR(50)
DECLARE @a VARCHAR(50),@b int
set @bh=ISNULL (( select top 1 bh FROM zzjg where pid=@id ORDER BY bh desc),'') --同级别已存在的编号
IF @bh='' --第一级 
BEGIN
   DECLARE @bh_s VARCHAR(50) 
   select @bh_s= bh FROM zzjg where id=@id 
   SET @bh=@bh_s+'00'
   --SET @b=0
END
SET @a=RIGHT(@bh,2)
--ELSE
--	BEGIN
--		--DECLARE @a VARCHAR(50),@b int
--		SET @a=RIGHT(@bh,2)
--		--SET @b=CONVERT(INT,@a)+1
--		--IF @b<10
--		--BEGIN
--		--	SET @bh=LEFT(@bh,LEN(@bh)-2)+'0'+CONVERT(VARCHAR,@b)
--		--END
--		--ELSE
--		--	BEGIN
--		--		SET @bh=LEFT(@bh,LEN(@bh)-2)+CONVERT(VARCHAR,@b)
--		--	END
--	END	
	
---------生成新编号 END--------

---------分隔单位编号 begin----------
declare @TempSca table(cid int,cbh VARCHAR(100),sort int )
declare @str varchar(50)
 set @str=@Companyid
 declare @next int 
 set @next=1
 while @next<=dbo.Get_StrArrayLength(@str,',')
   BEGIN
   	----------循环编号----------
   	--
   	SET @b=CONVERT(INT,@a)+@next
		IF @b<10
		BEGIN
			SET @bh=LEFT(@bh,LEN(@bh)-2)+'0'+CONVERT(VARCHAR,@b)
		END
		ELSE
			BEGIN
				SET @bh=LEFT(@bh,LEN(@bh)-2)+CONVERT(VARCHAR,@b)
			END   	
   	---------------------------
   	
     INSERT INTO @TempSca ( cid, cbh ,sort )
     
     SELECT * FROM (  select dbo.Get_StrArrayStrOfIndex(@str,',',@next) AS b,@bh AS a,@b AS c) AS n1
   WHERE b NOT IN (SELECT companyid FROM zzjg z ) --组织架构中相同的单位只能有一个
   
     set @next=@next+1
   end	

---------分隔单位编号 end----------



       INSERT INTO zzjg
		(
			
			pid,
			name,
			[level],
			sort,
			bh,
			flag,CompanyId
		)
		SELECT 
		
			@id,
			Company.CompanyName,
			 (SELECT [LEVEL] FROM zzjg WHERE id=@id )+1,
			Company.CompanyNumber,
			ts.cbh,
			1,ts.cid
	FROM Company,@TempSca ts WHERE Company.id=ts.cid
	
	           
  if @@ROWCOUNT<1
	begin
		set @errorSun=@errorSun+1		
		goto e;
	END
	
	--DECLARE @nid INT
	--set @nid =isnull(@@Identity ,0)

	INSERT INTO ManagementArea
		
	 (CompanyName,Startletter,CompanyPhone,CompanyFax,CompanyAddress,Zip,
	ContactPerson,ContactPhone,ContactEmail,WebAddress,Products,zid,flag,CompanyNumber)
	
		SELECT c.CompanyName,c.Startletter,c.CompanyPhone,c.CompanyFax,c.CompanyAddress,c.Zip,
		c.ContactPerson,c.ContactPhone,c.ContactEmail,c.WebAddress,c.Products,ts.cbh,1,ts.cbh
		 FROM Company c,@TempSca ts WHERE ts.cid=c.id
	
 --        DECLARE @nid INT
	--set @nid =isnull(@@Identity ,0)
  if @@ROWCOUNT<1
	begin
		set @errorSun=@errorSun+1		
		goto e;
	END
	
	
	
	DECLARE @nid INT
		SET @nid=(SELECT ma.Id FROM ManagementArea ma WHERE ma.zid =( SELECT z.bh
	                                                                FROM zzjg z WHERE z.id= @id))
	UPDATE company SET Mid = @nid FROM @TempSca ts WHERE ts.cid=company.id
	
	 if @@ROWCOUNT<1
	begin
		set @errorSun=@errorSun+1		
		goto e;
	END
	
	--Select isnull(@@Identity ,0) as 'RETUNR_V'
e:
    PRINT @errorSun
	if @errorSun>0
	begin
		rollback TRANSACTION	
	end
	else
	begin
		commit TRANSACTION
	END
	
	end
	RETURN

